import {
  Application,
  ParamsDictionary,
  PathParams,
  RequestHandler,
  RequestHandlerParams,
  RouteParameters
} from "express-serve-static-core";
import {Express} from "express";

export class BaseService {

  private readonly app: Express;

  private _mapping: string = '/';

  constructor(app: Express) {
    this.app = app;
  }

  static init(app: Express) {
    return new this(app);
  }

  protected mapping(mapping: string) {
    this._mapping = mapping;
    return this;
  }

  protected request(path: string, handler: RequestHandler) {
    return this.app.all(this._mapping + '/' + path, handler);
  }

  protected get(path: PathParams, subApplication: Application) {
    return this.app.get(this._mapping + '/' + path, subApplication);
  }

  protected post(path: string, handler: RequestHandler) {
    return this.app.post(this._mapping + '/' + path, handler);
  }

  protected put(path: string, handler: RequestHandler) {
    return this.app.put(this._mapping + '/' + path, handler);
  }

  protected delete(path: string, handler: RequestHandlerParams<string>) {
    return this.app.delete(this._mapping + '/' + path, handler);
  }

  protected patch(path: string, handler: RequestHandler) {
    return this.app.patch(this._mapping + '/' + path, handler);
  }

  protected options(path: string, handler: RequestHandler) {
    return this.app.options(this._mapping + '/' + path, handler);
  }
}
